#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "ohos_types.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "iot_gpio_ex.h"
#include "iot_pwm.h"

#define LED_C_GPIO 10
#define PWM_CHANNEL 1

static void PWMTask(void)
{
    unsigned int i;
    // 初始化 GPIO10
    IoTGpioInit(LED_C_GPIO);
    // 设置 GPIO10 的复用功能为 PWM
    IoTGpioSetFunc(LED_C_GPIO, IOT_GPIO_FUNC_GPIO_10_PWM1_OUT);
    // 初始化 PWM3
    IoTPwmInit(PWM_CHANNEL);
    while (1)
    {
        for (i = 1; i < 100; i++)
        {
            // 输出不同占空比，频率为 40KHz 的 PWM 波
            IoTPwmStart(PWM_CHANNEL, i, 40000);
            // 等待 10 毫秒
            usleep(10000);
        }
    }
}

void PWMExample(void)
{
    osThreadAttr_t attr;
    attr.name = "PWMTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 512;
    attr.priority = osPriorityNormal;
    if (osThreadNew((osThreadFunc_t) PWMTask, NULL, &attr) == NULL)
    {
        printf("Failed to create PWMTask!\n");
    }
}

SYS_RUN(PWMExample);
